﻿using System;
using System.Linq;
using SellAndBuy.Domain.Core;

namespace SellAndBuy.Domain.Entities
{
    /// <summary>
    /// Ad model
    /// </summary>
    public class Ad : PicturableBase, IAggregateRoot
    {
        private ItemCondition _condition;

        /// <summary>
        /// Private default constructor
        /// </summary>
        private Ad()
        {
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="customer">Customer</param>
        /// <param name="item">Item</param>
        public Ad(Customer customer, Item item)
        {
            Customer = customer;
            Item = item;
            CreationDate = DateTime.Now;
        }

        /// <summary>
        /// Customer
        /// </summary>
        public Customer Customer { get; private set; }

        /// <summary>
        /// Item definition
        /// </summary>
        public Item Item { get; private set; }

        /// <summary>
        /// Description
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// Price
        /// </summary>
        public decimal Price { get; set; }

        /// <summary>
        /// Creation date
        /// </summary>
        public DateTime CreationDate { get; private set; }

        /// <summary>
        /// Item Condition
        /// </summary>
        /// <exception cref="InvalidOperationException">This condition is not valid for this item type.</exception>
        public ItemCondition Condition 
        { 
            get { return _condition; } 
            set
            {
                if(!Item.Category.AvailableConditions.Contains(value))
                    throw new InvalidOperationException("This condition is not valid for this item type.");

                _condition = value;
            }
        }

        /// <summary>
        /// Change the item definition
        /// </summary>
        /// <param name="item"></param>
        public void ChangeItem(Item item)
        {
            if (item == null)
                throw new ArgumentNullException("item");

            if (Item == item)
                throw new InvalidOperationException("This item is already associated.");

            Item = item;
        }
    }
}